package net.seabook.games.tetris.model;


public class TBlock extends Piece {

	public TBlock(int x, int y) {
		Tile tile = null;
		for (int i = 0; i < 3; i++) {
			tile = new Tile(x + i * Tile.WIDTH, y);
			getBlocks().add(tile);
		}
		
		tile = new Tile(x + Tile.WIDTH, y - Tile.HEIGHT);
		getBlocks().add(tile);
	}


	@Override
	public void rotate() {
		switch (getrDirection()) {
		case LEFT:
			if (left2Up())setrDirection(RotateDirection.UP);
			break;
		case UP:
			if(up2Right()) setrDirection(RotateDirection.RIGHT);
			break;
		case RIGHT:
			if(right2Down()) setrDirection(RotateDirection.DOWN);
			
			break;
		case DOWN:
			if(down2Left()) setrDirection(RotateDirection.LEFT);
			break;
		default:
			break;
		}
	}
	
	private boolean down2Left() {
		Tile tile1 = this.getBlocks().get(0);
		int x1 = tile1.getX() - Tile.WIDTH;
		int y1 = tile1.getY() - Tile.HEIGHT;
		
		Tile tile2 = this.getBlocks().get(1);
		int x2 = tile2.getX();
		int y2 = tile2.getY();

		Tile tile3 = this.getBlocks().get(2);
		int x3 = tile3.getX() + Tile.WIDTH;
		int y3 = tile3.getY() + Tile.HEIGHT;
		
		Tile tile4 = this.getBlocks().get(3);
		int x4 = tile4.getX() + Tile.WIDTH;
		int y4 = tile4.getY() - Tile.HEIGHT;
		
		if (isRotateOutofBound(x1) || isRotateOutofBound(x2) || isRotateOutofBound(x3) || isRotateOutofBound(x4)) {
			return false;
		}
		
		tile1.setTilePosition(x1, y1);
		tile2.setTilePosition(x2, y2);
		tile3.setTilePosition(x3, y3);
		tile4.setTilePosition(x4, y4);
		
		return true;
	}

	private boolean right2Down() {
		Tile tile1 = this.getBlocks().get(0);
		int x1 = tile1.getX() - Tile.WIDTH;
		int y1 = tile1.getY() + Tile.HEIGHT;
		
		Tile tile2 = this.getBlocks().get(1);
		int x2 = tile2.getX();
		int y2 = tile2.getY();

		Tile tile3 = this.getBlocks().get(2);
		int x3 = tile3.getX() + Tile.WIDTH;
		int y3 = tile3.getY() - Tile.HEIGHT;
		
		Tile tile4 = this.getBlocks().get(3);
		int x4 = tile4.getX() - Tile.WIDTH;
		int y4 = tile4.getY() - Tile.HEIGHT;
		
		if (isRotateOutofBound(x1) || isRotateOutofBound(x2) || isRotateOutofBound(x3) || isRotateOutofBound(x4)) {
			return false;
		}
		
		tile1.setTilePosition(x1, y1);
		tile2.setTilePosition(x2, y2);
		tile3.setTilePosition(x3, y3);
		tile4.setTilePosition(x4, y4);
		
		return true;
	}

	private boolean up2Right() {
		Tile tile1 = this.getBlocks().get(0);
		int x1 = tile1.getX() + Tile.WIDTH;
		int y1 = tile1.getY() + Tile.HEIGHT;
		
		Tile tile2 = this.getBlocks().get(1);
		int x2 = tile2.getX();
		int y2 = tile2.getY();

		Tile tile3 = this.getBlocks().get(2);
		int x3 = tile3.getX() - Tile.WIDTH;
		int y3 = tile3.getY() - Tile.HEIGHT;
		
		Tile tile4 = this.getBlocks().get(3);
		int x4 = tile4.getX() - Tile.WIDTH;
		int y4 = tile4.getY() + Tile.HEIGHT;
		
		if (isRotateOutofBound(x1) || isRotateOutofBound(x2) || isRotateOutofBound(x3) || isRotateOutofBound(x4)) {
			return false;
		}
		
		tile1.setTilePosition(x1, y1);
		tile2.setTilePosition(x2, y2);
		tile3.setTilePosition(x3, y3);
		tile4.setTilePosition(x4, y4);
		
		return true;
	}

	private boolean left2Up() {
		Tile tile1 = this.getBlocks().get(0);
		int x1 = tile1.getX() + Tile.WIDTH;
		int y1 = tile1.getY() - Tile.HEIGHT;
		
		Tile tile2 = this.getBlocks().get(1);
		int x2 = tile2.getX();
		int y2 = tile2.getY();

		Tile tile3 = this.getBlocks().get(2);
		int x3 = tile3.getX() - Tile.WIDTH;
		int y3 = tile3.getY() + Tile.HEIGHT;
		
		Tile tile4 = this.getBlocks().get(3);
		int x4 = tile4.getX() + Tile.WIDTH;
		int y4 = tile4.getY() + Tile.HEIGHT;
		
		if (isRotateOutofBound(x1) || isRotateOutofBound(x2) || isRotateOutofBound(x3) || isRotateOutofBound(x4)) {
			return false;
		}
		
		tile1.setTilePosition(x1, y1);
		tile2.setTilePosition(x2, y2);
		tile3.setTilePosition(x3, y3);
		tile4.setTilePosition(x4, y4);
		
		return true;
	}

}
